package midend.IR_Instruction;

import midend.IR_Type.IR_Int1;
import midend.IR_Type.IR_Type;
import midend.IR_Value.IR_BaseBlock;
import midend.IR_Value.IR_FuncBlock;
import midend.IR_Value.IR_Local_Value;
import midend.IR_Value.IR_Value;

import java.util.ArrayList;
import java.util.List;

public class Zext_Instruction extends Instruction{
    public Zext_Instruction(List<IR_Value> op, IR_BaseBlock bb) {
        super(op, bb);
        addValue(get_op_index(1));
    }

    public void updateop(int index, IR_Value newValue) {
        set_Op(index + 1, newValue);
    }
    @Override
    public String toString() {
        // op  [ret, Rval,Lval]
        return get_op().get(0).get_ValueName() + " = zext " + IR_Int1.IR_Int1().get_TypeName()
                + " " + get_op().get(1).get_ValueName() + " to " + get_op().get(2).get_Type().get_TypeName();
    }


    @Override
    public Zext_Instruction clone(IR_BaseBlock block) {
        String copy_name = ((IR_Local_Value) get_op().get(0)).get_Local_Identity();
        IR_Type copy_type = get_op().get(0).get_Type();
        IR_Value res = new IR_Local_Value(copy_name, copy_type);
        ArrayList<IR_Value> copy_ops = new ArrayList<>();
        copy_ops.add(res);
        for (int i = 1 ; i< get_op().size() ; i++) {
            IR_Value copy_op  = get_op().get(i);
            if(IR_FuncBlock.cloneMap_Value.containsKey(copy_op)) {
                copy_op = IR_FuncBlock.cloneMap_Value.get(copy_op);
            }
            copy_ops.add(copy_op);
        }
        Zext_Instruction copy_ins = new Zext_Instruction(copy_ops, block);
        return copy_ins;
    }

}
